import numpy as np
proper=None
picked=False
picked_id=0
picked_xy=[0,0]
x=0;last_x=0
y=0;last_y=0
canvas=None
photos=None
def update_piece(id,new_x,new_y):
    canvas.delete(proper[id,4])
    top_id=canvas.create_image(new_x,new_y,image=photos[id],tags=("piece",))
    proper[picked_id,4]=top_id
#获得鼠标位置
def get_mouse_xy(event):
    global x,y,last_x,last_y
    last_x,last_y=(x,y)
    x,y = (event.x,event.y)

#删除并重新绘制选中的图片
def flash_piece(event):
    if picked:
        update_piece(picked_id,x+picked_xy[0],y+picked_xy[1])
        #print(top_id) 
    else:
        dx,dy=(x-last_x,y-last_y)
        canvas.move("piece",dx,dy)
        proper[:,(0,1,6)]+=dx
        proper[:,(2,3,7)]+=dy

#删除刚绘制的图片
def release_piece(event):
    global picked
    if picked:
        picked=False
        rx=abs(int((proper[picked_id,0]-proper[picked_id,1])/2))
        ry=abs(int((proper[picked_id,2]-proper[picked_id,3])/2))

        proper[picked_id,0]=x+picked_xy[0]-rx
        proper[picked_id,1]=x+picked_xy[0]+rx
        proper[picked_id,2]=y+picked_xy[1]-ry
        proper[picked_id,3]=y+picked_xy[1]+ry
        print(proper)
        thr=30
        current=np.abs(proper[picked_id,(0,2)]-proper[picked_id,(6,7)])>thr
        if current.any():
            proper[picked_id,5]=1
        else:
            proper[picked_id,5]=0
            update_piece(picked_id,proper[picked_id,6]+rx,proper[picked_id,7]+ry)
            if not proper[:,5].any():
                print("I MISS YOU!!!")

#根据鼠标位置，确定当前区域，即选中的图片编号
def pick_piece(event):
    global picked,picked_id,picked_xy
    round_pieces_idx=(x>=proper[:,0])&(x<proper[:,1])&(y>=proper[:,2])&(y<proper[:,3])&(1==proper[:,5])
    round_pieces_idx=(np.nonzero(round_pieces_idx))[0]

    if round_pieces_idx.size !=0:
        max_id=np.argmax(proper[round_pieces_idx,4])
        max_id=round_pieces_idx[max_id]

        picked=True
        picked_id=max_id
        picked_xy=[int((proper[max_id,0]+proper[max_id,1])/2-x),int((proper[max_id,2]+proper[max_id,3])/2-y)]





